package cn.tedu.voice.system.service.impl;

import cn.tedu.voice.system.ex.ServiceException;
import cn.tedu.voice.system.mapper.VoiceMapper;
import cn.tedu.voice.system.mvc.JsonPage;
import cn.tedu.voice.system.mvc.ServiceCode;
import cn.tedu.voice.system.pojo.dto.VoiceAddNewDTO;
import cn.tedu.voice.system.pojo.dto.VoiceUpdateDTO;
import cn.tedu.voice.system.pojo.entitly.Voice;
import cn.tedu.voice.system.pojo.vo.VoiceListVO;
import cn.tedu.voice.system.service.IVoiceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 处理语音管理的实现类
 *
 * @author java@ccl.cn
 * @version 0.0.1
 */
@Slf4j
@Service
public class VoiceServiceImpl implements IVoiceService {

    @Autowired
    private VoiceMapper voiceMapper;

    @Override
    public void addNew(VoiceAddNewDTO voiceAddNewDTO) {
        log.debug("开始处理上传语音的业务，参数：{}", voiceAddNewDTO);
        // 检查语音编号是否已经被占用
        Integer voiceNumber = voiceAddNewDTO.getVoiceNumber();
        int number = voiceMapper.countByvoiceNumber(voiceNumber);
        if (number > 0) {
            throw new ServiceException(
                    "上传失败，编号被占用！", ServiceCode.ERR_CONFLICT);
        }
        Voice voice = new Voice();
        LocalDateTime now = LocalDateTime.now();
        voice.setUploadTime(now);
        BeanUtils.copyProperties(voiceAddNewDTO, voice);
        int rows = voiceMapper.insert(voice);
        if (rows != 1) {
            throw new ServiceException("上传失败，服务器忙，请稍后重试！", ServiceCode.ERR_INSERT);
        }
    }

    @Override
    public void delete(Long[] ids) {
        log.debug("开始处理删除语音的业务，参数：{}", ids);
        // 检查语音信息是否存在
        int voice = voiceMapper.countByIds(ids);
        if (voice <= 0) {
            throw new ServiceException("删除失败，信息不存在！", ServiceCode.ERR_CONFLICT);
        }

        int rows = voiceMapper.deleteByIds(ids);
        if (rows == 0) {
            throw new ServiceException("删除失败，服务器忙，请稍后重试！", ServiceCode.ERR_DELETE);
        }

    }


    @Override
    public void update(Integer voiceNumber, VoiceUpdateDTO voiceUpdateDTO) {
        log.debug("开始处理修改语音的业务，参数：{}", voiceNumber, voiceUpdateDTO);
        //修改最新的时间
        LocalDateTime now = LocalDateTime.now();
        Voice voice = new Voice();
        voice.setUploadTime(now);
        System.out.println(voiceNumber);
        // 检查语音信息是否存在
        int voiceResult = voiceMapper.countByvoiceNumber(voiceNumber);
        if (voiceResult <=0) {
            throw new ServiceException("修改失败，信息不存在！", ServiceCode.ERR_CONFLICT);
        }
        //检查新的名称是否已经被相同id的数据占用
        int count = voiceMapper.countByvoiceNameAndNotId(
                voiceUpdateDTO.getId(), voiceUpdateDTO.getVoiceName());
        if (count > 0) {
            throw new ServiceException("修改失败，该名称已被占用！", ServiceCode.ERR_CONFLICT);
        }

        BeanUtils.copyProperties(voiceUpdateDTO, voice);
        voice.setId(voice.getId());
        log.debug("执行修改数据，参数：{}", voice);
        voiceMapper.update(voice);
    }

    @Override
    public List<VoiceListVO> list() {
        log.debug("开始处理查询列表业务，无参数");
        List<VoiceListVO> list = voiceMapper.list();
        log.debug("即将返回查询结果：{}", list);
        return list;
    }

    @Override
    public JsonPage<VoiceListVO> list(Integer page, Integer pageSize) {
        System.out.println(page);
        System.out.println(pageSize);
        PageHelper.startPage(page,pageSize);
        List<VoiceListVO> list = voiceMapper.list();
        return JsonPage.restPage(new PageInfo<>(list));
    }


}
